package com.weizhu.swufer.core.filter;

import com.weizhu.swufer.constant.SwuferConstant;
import com.weizhu.swufer.core.annotation.SwuferFilter;
import com.weizhu.swufer.core.entity.InvokeParam;
import com.weizhu.swufer.core.exception.RpcBizException;
import com.weizhu.swufer.core.exception.RpcException;
import com.weizhu.swufer.core.metrics.MetricsManager;
import com.weizhu.swufer.core.metrics.RpcMetrics;

//@SwuferFilter(type = {SwuferConstant.FILTER_TYPE_CLIENT},order = -1)
public class MonitorFilter implements Filter{
    @Override
    public Object doFilter(InvokeParam invokeParam, FilterChain filterChain) throws Exception {

        RpcMetrics methodRpcMetrics = MetricsManager.getMethodRpcMetrics(invokeParam.getServiceMethodName());
        methodRpcMetrics.beginStat();
        try {
            Object o = filterChain.doFilter(invokeParam);
        }catch (RpcException e){
            methodRpcMetrics.endStat(false);
        }catch (RpcBizException e){
            methodRpcMetrics.endStat(true);
        }

        return null;
    }
}
